package com.augurit.wf.common.extmodel;

import java.util.ArrayList;
import java.util.List;

import org.jbpm.api.ProcessDefinition;

import com.augurit.wf.common.service.IWfService;
import com.augurit.wf.mgmt.web.form.WfProcessVersionForm;

/**
 * 流程版本树上的流程节点
 */
public class ProcessNameTreeNode extends ProcessReleaseTreeNode{
	
	/**
	 * 构造函数
	 * @param type
	 * @param value
	 */
	public ProcessNameTreeNode(String value){
		this.type = ProcessReleaseTreeNode.PROCESS_NODE;
		this.iconCls = ProcessReleaseTreeNode.CLS_PROCESS_NAME;
		this.value = value;
		
	}
	
	@Override
	public List<ProcessReleaseTreeNode> getChildNodes(IWfService wfService){
		List<ProcessDefinition> list = wfService.getProcDefByProcDefKey(this.getValue());
		return this.convertToTreeNode(list);
	}
	
	/**
	 * 转换为
	 * @param list
	 * @return
	 */
	private List<ProcessReleaseTreeNode> convertToTreeNode(List<ProcessDefinition> list){
		List<ProcessReleaseTreeNode> nodes = null;
		if(list != null && list.size() > 0){
			nodes = new ArrayList<ProcessReleaseTreeNode>();
			
			for(int i = 0; i < list.size(); i++){
				ProcessDefinition procDef = list.get(i);
				ProcessReleaseTreeNode treeNode = new ProcessVersionTreeNode(String.valueOf(procDef.getVersion()));
				String version = WfProcessVersionForm.formatVersion(procDef.getVersion());
				if(i == 0){
					version = WfProcessVersionForm.formatToTheLatestVersion(procDef.getVersion());
				}
				treeNode.setText(version);
				nodes.add(treeNode);
			}
		}
		return nodes;
	}
}
